#!/usr/bin/env python
# -*- coding:utf-8 -*-

from db.dbConn import DBConn
from logger import logger


class MySQLBase(object):
    """MySQL数据库类
    输入：数据源，SQL语句
    """

    def __init__(self, datasouce, sqlmodle):
        """Init MySQLBase"""
        self.sql = sqlmodle
        self.conn, self.cursor = DBConn(**datasouce).MySQLConn()

    def check(self):
        """检测数据库连通性"""
        try:
            self.cursor.execute("show databases;")
            logger.error("数据库连接成功")
            self.close()
        except Exception as e:
            logger.error("数据库连接异常，请查看")
            logger.error(e)
            exit(-1)

    def query(self):
        """查询MySQL数据 insert、update、delete返回None"""
        try:
            self.cursor.execute(self.sql)
            res = self.cursor.fetchall()
            self.close()
            return res
        except Exception as e:
            logger.error("SQL执行失败，执行语句为：{}".format(self.sql))
            logger.error(e)
            return 0

    def insert(self):
        """插入MySQL数据"""
        try:
            self.cursor.execute(self.sql)
            self.conn.commit()
            self.close()
            return 1
        except Exception as e:
            logger.error("SQL执行失败，执行语句为：{}".format(self.sql))
            logger.error(e)
            return 0

    def update(self):
        """修改MySQL数据"""
        try:
            self.cursor.execute(self.sql)
            self.conn.commit()
            self.close()
            return 1
        except Exception as e:
            logger.error("SQL执行失败，执行语句为：{}".format(self.sql))
            logger.error(e)
            return 0

    def delete(self):
        """删除MySQL数据"""
        try:
            self.cursor.execute(self.sql)
            self.conn.commit()
            self.close()
            return 1
        except Exception as e:
            logger.error("SQL执行失败，执行语句为：{}".format(self.sql))
            logger.error(e)
            return 0

    def truncate(self):
        """清空MySQL表"""
        try:
            self.cursor.execute(self.sql)
            self.conn.commit()
            self.close()
            return 1
        except Exception as e:
            logger.error("SQL执行失败，执行语句为：{}".format(self.sql))
            logger.error(e)
            return 0

    def close(self):
        """关闭连接"""
        self.cursor.close()
        self.conn.close()


class InfluxDBBase(object):
    """数据库查询类
    输入：数据源，SQL语句
    """

    def __init__(self):
        """Init InfluxBase
        """
        self.datasouce = ''
        self.sql = ''

    def query(self, datasouce, sqlmodle):
        """查询Influxdb数据"""
        self.datasouce = datasouce
        self.sql = sqlmodle
        try:
            client = DBConn(**self.datasouce).InfluxConn()
            result = client.query(self.sql)
            return result
        except Exception as e:
            logger.error("influx 连接失败，请稍后重试。")
            logger.error(e)
            return 0

    def insert(self, datasouce, points):
        """插入Influxdb数据
           points: JSON串
        """
        self.datasouce = datasouce
        try:
            client = DBConn(**self.datasouce).InfluxConn()
            client.write_points(points=points, time_precision='s', batch_size=100)
            return 1
        except Exception as e:
            logger.error("influx 写入失败，请查看。")
            logger.error(e)
            return 0
